﻿using System;
using log4net;
using Metozis.Model;
using Metozis.Repo;

namespace Metozis.Facade
{
    internal class OrderFacade : IOrderFacade
    {
        private readonly IReceipientInvoiceRepo invoiceRepo ;
        private readonly IProductRepo productRepo;
        private ILog log;

        public OrderFacade(ILog log, IReceipientInvoiceRepo invoiceRepo, IProductRepo productRepo)
        {
            this.log = log;
            this.invoiceRepo = invoiceRepo;
            this.productRepo = productRepo;
        }

        public Order GetOrderByInvoiceId(int id)
        {
            log.InfoFormat("Metozis.Facade.OrderFacade.GetOrderByInvoiceId({0})", id);
            try
            {
                ReceipientInvoice invoice = invoiceRepo.GetInvoiceById(id);
                Product product = productRepo.GetProductById(invoice.ProductId);

                return new Order { InvoiceId = invoice.Id, ProductName = product.Name };
            }
            catch (Exception caught)
            {
                throw new ApplicationException(caught.Message, caught);
            }
            
        }
    }
}